package net.jeanhwea.gold.model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.mysql.jdbc.Connection;

import net.jeanhwea.gold.bean.Item;

/**
 * A list operation about book item
 * 
 * @author Jeanhwea
 *
 */

public class ItemPeer {
  
  public static ArrayList <Item> searchItemById(DataManager dataManager, int id){
    ArrayList <Item> items = new ArrayList<Item>();
    Connection con = dataManager.getConnection();
    
    if(con != null) {
      try {
        Statement s = con.createStatement();
        String query = "SELECT *  FROM inf WHERE id = "+id;
        try{
          ResultSet rs = s.executeQuery(query);
          try{
            while (rs.next()){
              Item item = new Item();
              item.setID(Integer.parseInt(rs.getString(1)));
              item.setPrice(Double.parseDouble(rs.getString(2)));
              item.setTime(rs.getString(3));
              item.setDate(rs.getString(4));
            }
          } finally{
            rs.close();
          }
          
        }finally{ 
          s.close(); 
        }
      } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.out.println("Could not search for. " + e.getMessage());
      } finally{
        dataManager.closeConnection(con);
      }
    }
    
    return items;
  }
  
}
